package com.mzj.saas.commons;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author chenshifan
 *
 */
public class HtmlUtils {
	
	/**
	 * 匹配需要替换内容的url
	 * @return
	 */
	public static List<String> matchedURLs(){
		List <String> urls = new ArrayList<String>();
		              urls.add("http://uat.files.mzjmedia.com");
		              urls.add("http://dev.files.mzjmedia.com");
		              urls.add("http://files.mzjmedia.com");
		              urls.add("http://hxoss.mzj02.top");
		              urls.add("https://hxoss.mzj02.top");
	    return urls;
	}
	/**
	 * 替换指定标签的属性和值
	 * @param str 需要处理的字符串
	 * @param tag 标签名称
	 * @param tagAttrib 要替换的标签属性值
	 * @param appendStr 追加的内容
	 * @matchedURLs 匹配的url
	 * @return
	 */
	public static String replaceHtmlTag(String str, String tag, String tagAttrib,String appendStr,List<String> matchedURLs) {
		String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
		String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
		Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );
		Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );   
		Matcher matcherForTag = patternForTag.matcher(str);
		StringBuffer sb = new StringBuffer();
		boolean result = matcherForTag.find();
		while (result) {
			StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");
		    Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
        	if (matcherForAttrib.find()) {
        		String attributeStr = matcherForAttrib.group(1);
        		if(matchedURLs != null && matchedURLs.size() >0){
        			for (String url : matchedURLs) {
        				if(attributeStr.contains(url)){
        					String newsURLStr = "";
        					if(attributeStr.contains("?")){
        						newsURLStr = "&" + appendStr;
        					}else{
        						newsURLStr = "?" + appendStr;
        					}
        					String repStr = "src=\""+attributeStr+newsURLStr+"\"";
        					matcherForAttrib.appendReplacement(sbreplace, repStr);
        				}
					}
        		}
        	
        	}
        	matcherForAttrib.appendTail(sbreplace);
        	matcherForTag.appendReplacement(sb, sbreplace.toString());
        	result = matcherForTag.find();
		}
		matcherForTag.appendTail(sb);         
		return sb.toString();
	}
	
	/**
	 * 替换指定标签的属性和值
	 * @param str 需要处理的字符串
	 * @param tag 标签名称
	 * @param tagAttrib 要替换的标签属性值
	 * @param oldStr 原始的内容
	 * @param newsStr 替换的内容
	 * @matchedURLs 匹配的url
	 * @return
	 */
	public static String replaceHtmlTagStr(String str, String tag, String tagAttrib,String oldStr,String newsStr,List<String> matchedURLs) {
		String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
		String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
		Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );
		Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );   
		Matcher matcherForTag = patternForTag.matcher(str);
		StringBuffer sb = new StringBuffer();
		boolean result = matcherForTag.find();
		while (result) {
			StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");
		    Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
        	if (matcherForAttrib.find()) {
        		String attributeStr = matcherForAttrib.group(1);
        		if(matchedURLs != null && matchedURLs.size() >0){
        			for (String url : matchedURLs) {
        				if(attributeStr.contains(url)){
        					String newStr = attributeStr.replace(oldStr, newsStr);
        					String repStr = "src=\""+newStr+"\"";
        					System.out.println(repStr);
        					matcherForAttrib.appendReplacement(sbreplace, repStr);
        				}
					}
        		}
        	
        	}
        	matcherForAttrib.appendTail(sbreplace);
        	matcherForTag.appendReplacement(sb, sbreplace.toString());
        	result = matcherForTag.find();
		}
		matcherForTag.appendTail(sb);         
		return sb.toString();
	}
	
	/**
	 * 获取html特定标签的属性值
	 * @param str 需要处理的字符串
	 * @param tag 标签名称
	 * @param tagAttrib tag属性名称
	 * @matchedURLs 匹配的url
	 * @return
	 */
	public static List<String> getHtmlTagValues(String str, String tag, String tagAttrib,List<String> matchedURLs) {
		List<String> list = new ArrayList<String>();
		String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
		String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
		Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );
		Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );   
		Matcher matcherForTag = patternForTag.matcher(str);
		StringBuffer sb = new StringBuffer();
		boolean result = matcherForTag.find();
		while (result) {
		    Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
        	if (matcherForAttrib.find()) {
        		String attributeStr = matcherForAttrib.group(1);
        		if(matchedURLs != null && matchedURLs.size() >0){
        			for (String url : matchedURLs) {
        				if(attributeStr.contains(url)){
        					list.add(attributeStr);
					    }
        		}
        	
        	}
      
        	result = matcherForTag.find();
		}
		sb.toString();
		
	   }
		return list;
	}
    
   public static void main(String[] args) {
    	StringBuffer content = new StringBuffer();
    	content.append("<ul class=\"imgBox\"><li><img id=\"160424\" src=\"http://files.mzjmedia.com/HX/160424/1-160424120T1-50.jpg?a=1\" class=\"src_class\"></li>");
    	content.append("<li><img id=\"150628\" src=\"http://files666.mzjmedia.com/allimg/150628/1-15062Q12247.jpg\" class=\"src_class\"></li></ul>");
    	content.append("<li><img id=\"150628\" src=\"http://dev.files.mzjmedia.com/HX/150628/1-15062Q12247.jpg\" class=\"src_class\"></li></ul>");
        
    	//System.out.println("原始字符串为:"+content.toString());
       // String newStr = replaceHtmlTag(content.toString(), "img", "src","x-oss-process=image/resize,m_lfit,w_100",matchedURLs());
       // System.out.println("       替换后为:"+newStr);()
    	//List<String> list  = getHtmlTagValues(content.toString(), "img", "src",matchedURLs());
    	//for (String str : list) {
		//	System.out.println(str);
		//}
    	//String newStr = replaceHtmlTagStr(content.toString(), "img", "src","HX","hx",matchedURLs());
    	//System.out.println(newStr);
     
    } 
    
	
}
